package hos.kotlin


/**
 * <p>Title: ListExt </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @date : 2022/7/4 11:30
 * @version : 1.0
 */
/**
 * 将map转换为List
 *
 * @return List
 */
fun <K, V> Map<K, V>?.toListKey(): MutableList<K> {
    if (this == null || this.isEmpty()) {
        return mutableListOf()
    }
    val list = mutableListOf<K>()
    list.addAll(this.keys)
    return list
}

/**
 * 将map转换为List
 *
 * @return List
 */
fun <K, V> Map<K, V>?.toListValue(): MutableList<V> {
    if (this == null || this.isEmpty()) {
        return mutableListOf()
    }
    val list = mutableListOf<V>()
    val keySet = this.keys
    for (key in keySet) {
        val item = this[key] ?: continue
        list.add(item)
    }
    return list
}

fun <K, V> Map<K, V>.urlParam(): String {
    val queryKeys = this.keys
    val builder = StringBuilder()
    for (queryKey in queryKeys) {
        if (builder.length != 0) {
            builder.append("&")
        }
        val value: Any? = this[queryKey]
        if (value != null) {
            builder.append(queryKey).append("=").append(value)
        } else {
            builder.append(queryKey).append("=").append("")
        }
    }
    return builder.toString()
}

//<editor-fold desc="T">
fun <K, V> Map<K, V>?.getIfNull(key: K, value: V? = null): V? {
    if (this == null || this.isEmpty() || !this.containsKey(key)) {
        return value
    }
    val v = this[key]
    return if (v == null || "null" == v || "NULL" == v || "Null" == v) {
        value
    } else {
        v
    }
}

fun <K, V> Map<K, V>?.getIfNull(key: K, value: () -> V?): V? {
    if (this == null || this.isEmpty() || !this.containsKey(key)) {
        return value.invoke()
    }
    val v = this[key]
    return if (v == null || "null" == v || "NULL" == v || "Null" == v) {
        value.invoke()
    } else {
        v
    }
}

fun <K, V> Map<K, V>?.toNull(key: K): V? {
    return this.getIfNull<K, V>(key, null)
}
//</editor-fold>

//<editor-fold desc="String">
fun <K, V> Map<K, V>?.toNullString(key: K): String? {
    val target = this.toNull<K, V>(key) ?: return null
    return target.toString()
}

fun <K, V> Map<K, V>?.empty(
    key: K,
    value: String = "",
    start: String = "",
    end: String = ""
): String {
    val target = toNullString<K, V>(key) ?: return start + value + end
    return start + target + end
}

fun <K, V> Map<K, V>?.empty(
    key: K,
    start: String = "",
    end: String = "",
    value: () -> String
): String {
    val target = toNullString<K, V>(key) ?: return start + value + end
    return start + target + end
}

fun <K, V> Map<K, V>?.divide(key: K): String {
    return empty<K, V>(key, "-")
}


fun <K, V> Map<K, V>?.rate(key: K, value: String = ""): String {
    return empty<K, V>(key, value, end = "%")
}

fun <K, V> Map<K, V>?.like(key: K, value: String = ""): String {
    val like = toNullString<K, V>(key) ?: return "%$value%"
    return "%$like%"
}
//</editor-fold>

//<editor-fold desc="Number">
fun <K, V> Map<K, V>?.asInteger(key: K, value: Int = 0): Int = toNull<K, V>(key).asInteger(value)

fun <K, V> Map<K, V>?.toNullInteger(key: K): Int? = toNull<K, V>(key).toNullInteger()

fun <K, V> Map<K, V>?.asIntegerString(key: K, value: Int = 0): String =
    this.toNull<K, V>(key).asIntegerString(value)

fun <K, V> Map<K, V>?.toNullIntegerString(key: K): String? =
    this.toNull<K, V>(key).toNullIntegerString()

fun <K, V> Map<K, V>?.asDouble(key: K, value: Double = 0.0): Double =
    toNull<K, V>(key).asDouble(value)

fun <K, V> Map<K, V>?.toNullDouble(key: K): Double? = toNull<K, V>(key).toNullDouble()

fun <K, V> Map<K, V>?.asDoubleString(key: K, value: Double = 0.0): String =
    this.toNull<K, V>(key).asDoubleString(value)

fun <K, V> Map<K, V>?.toNullDoubleString(key: K): String? =
    this.toNull<K, V>(key).toNullDoubleString()

fun <K, V> Map<K, V>?.asLong(key: K, value: Long = 0L): Long = toNull<K, V>(key).asLong(value)

fun <K, V> Map<K, V>?.toNullLong(key: K): Long? = toNull<K, V>(key).toNullLong()

fun <K, V> Map<K, V>?.asLongString(key: K, value: Long = 0L): String =
    this.toNull<K, V>(key).asLongString(value)

fun <K, V> Map<K, V>?.toNullLongString(key: K): String? =
    this.toNull<K, V>(key).toNullLongString()

fun <K, V> Map<K, V>?.asFloat(key: K, value: Float = 0f): Float = toNull<K, V>(key).asFloat(value)

fun <K, V> Map<K, V>?.toNullFloat(key: K): Float? = toNull<K, V>(key).toNullFloat()

fun <K, V> Map<K, V>?.asFloatString(key: K, value: Float = 0f): String =
    this.toNull<K, V>(key).asFloatString(value)

fun <K, V> Map<K, V>?.toNullFloatString(key: K): String? =
    this.toNull<K, V>(key).toNullFloatString()

fun <K, V> Map<K, V>?.asBoolean(key: K, value: Boolean = false): Boolean =
    toNull<K, V>(key).asBoolean(value)

fun <K, V> Map<K, V>?.toNullBoolean(key: K): Boolean? = toNull<K, V>(key).toNullBoolean()

fun <K, V> Map<K, V>?.asBooleanString(key: K, value: Boolean = false): String =
    this.toNull<K, V>(key).asBooleanString(value)

fun <K, V> Map<K, V>?.toNullBooleanString(key: K): String? =
    this.toNull<K, V>(key).toNullBooleanString()
//</editor-fold>


//<editor-fold desc="Length">
fun <K, V> Map<K, V>?.addLength(key: K, defaultNull: String = "", allLength: Int): String {
    return (toNullString(key) ?: defaultNull).addLength(allLength)
}

fun <K, V> Map<K, V>?.addLengthCenter(
    key: K, defaultNull: String?, allLength: Int
): String {
    return (toNullString(key) ?: defaultNull).addLengthCenter(allLength)
}
//</editor-fold>



